//
//  MTHomeViewModel.swift
//  MTWeiBo
//
//  Created by user on 2017/7/26.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit

class MTHomeViewModel: NSObject {

    //数据源
    lazy var statusList:[MTSatusViewModel] = [MTSatusViewModel]()

    //加载微博数据
    //  callBack: 闭包回调， true表示成功回调， false表示失败的回调
    func loadData(callBack: @escaping (Bool)->()) {
        //令牌
        let token = MTUserAccountViewModel.shardViewModel.accessToken!
        HMNetworkTools.sharedTools.requestStatuses(access_token: token) { (respone, error) in
            if error != nil {
                print("error\(error!)")
                callBack(false)
                return
            }
            //执行到此说明成功
            let dict = respone as! [String: Any]
            //获取微博字典数组
            let statusesDictArr = dict["statuses"] as! [[String: Any]]
            //  使用YYModel完成字典转模型
            let statuseModel = NSArray.yy_modelArray(with: MTStatuses.self, json: statusesDictArr) as! [MTStatuses]
            
            var tempArr = [MTSatusViewModel]()
            //  遍历模型数组转成对应的ViewMode的数组
            for statusModel in statuseModel {
                let viweModel = MTSatusViewModel()
                //关联viewmodel
                viweModel.status = statusModel
                tempArr.append(viweModel)
            }
            
            //赋值
            self.statusList = tempArr
            
            callBack(true)
        
        }
    }
    
    
}



